package com.oocl.euc.core.maintenance.controller;

import com.oocl.euc.core.builder.TeamBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Team;
import com.oocl.euc.core.maintenance.repository.TeamRepository;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

public class TeamControllerIT extends ITTestBase {
    @Autowired
    TeamRepository teamRepository;

    @Autowired
    TeamBuilder teamBuilder;

    @After
    public void reset() {
        teamRepository.deleteAll();
    }

    @Test
    public void should_return_all_teams_given_url_is_all_when_exist_one_team() throws Exception {
        Team team = teamBuilder.defaultBuilder()
                .withTeamName("TEST TEAM")
                .withTeamDesc("TEST DESC")
                .saveToDB();
        mockMvc.perform(get("/rest/teamList/all").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", Matchers.hasSize(1)))
                .andExpect(jsonPath("$[0].name", is(team.getName())))
                .andExpect(jsonPath("$[0].desc", is(team.getDesc())));
    }

    @Test
    public void should_return_all_teams_given_url_is_all_when_exist_multiple_teams() throws Exception {
        Team team1 = teamBuilder.defaultBuilder()
                .withTeamName("TEST TEAM")
                .withTeamDesc("TEST DESC")
                .saveToDB();
        Team team2 = teamBuilder.defaultBuilder()
                .withTeamName("TEST TEAM2")
                .withTeamDesc("TEST DESC2")
                .saveToDB();
        mockMvc.perform(get("/rest/teamList/all").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", Matchers.hasSize(2)))
                .andExpect(jsonPath("$[0].name", is(team1.getName())))
                .andExpect(jsonPath("$[0].desc", is(team1.getDesc())))
                .andExpect(jsonPath("$[1].name", is(team2.getName())))
                .andExpect(jsonPath("$[1].name", is(team2.getName())));
    }

    @Test
    public void should_return_team_with_given_id_in_url() throws Exception {
        Team team = teamBuilder.defaultBuilder()
                .withTeamName("TEST TEAM")
                .withTeamDesc("TEST DESC")
                .saveToDB();
        mockMvc.perform(get("/rest/teamList/{id}", team.getId()).accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$.name", is(team.getName())));
    }
}